//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssUseOrderDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssUseOrder;
import com.aidong.model.wills.PssUseOrderList;
import com.aidong.model.wills.PssUseOrderTotal;
import com.aidong.util.ExportXls;
import com.aidong.util.StockUtil;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"useOrder"})
public class UseOrderController extends PopProductController {
    @Resource
    PssUseOrderDao pssUseOrderDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    StockUtil stockUtil;

    public UseOrderController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("pss/useorder/list")).addObject("shopName", this.getTypeList("shopName"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "lingyongDateFrom",required = false) String lingyongDateFrom, @RequestParam(value = "lingyongDateTo",required = false) String lingyongDateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        if(!this.chkNull(shopCode)) {
            cd.eq("UseShop", shopCode);
        }

        if(!this.chkNull(lingyongDateFrom)) {
            cd.ge("UseDate", this.parseDate(lingyongDateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(lingyongDateTo)) {
            cd.le("UseDate", this.parseDate(lingyongDateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.pssUseOrderDao.getLstUseOrder(pg, cd);
        ModelAndView mv = new ModelAndView("pss/useorder/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("ProductcodeList", this.getTypeList("ProductcodeList"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("shopCode") String shopCode) {
        PssUseOrder pssUseOrder = new PssUseOrder();
        pssUseOrder.setUseShop(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pss/useorder/edit");
        mv.addObject("pssUseOrder", pssUseOrder);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("useType", this.getTypeList("useType"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssUseOrder") PssUseOrder pssUseOrder, @RequestParam(value = "useShop",required = false) String useShop, @RequestParam(value = "useType",required = false) String useType, @RequestParam(value = "note",required = false) String note, Products products) {
        if(this.chkNull(useShop)) {
            return this.getResult(false, "请选择门店");
        } else if(this.chkNull(useType)) {
            return this.getResult(false, "请选择领用类型");
        } else if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请选择要领用的商品");
        } else {
            try {
                Date e = new Date();
                pssUseOrder.setSendShop(useShop);
                pssUseOrder.setUseDate(e);
                pssUseOrder.setCreateUser(this.getLoginUserCode());
                pssUseOrder.setCreateTime(e);
                pssUseOrder.setUpdateTime(e);
                PssUseOrderList pss = null;
                ArrayList lst = new ArrayList();
                Iterator i$ = products.getLstProduct().iterator();

                while(i$.hasNext()) {
                    PssPopProduct pc = (PssPopProduct)i$.next();
                    pss = new PssUseOrderList();
                    if(!this.chkNull(pc.getProductCode())) {
                        if(!this.stockUtil.checkStock(useShop, pc.getProductCode(), pc.getQty())) {
                            return this.getResult(false, "请确认商品：" + pc.getProductCode() + "的库存！");
                        }

                        pss.setProductCode(pc.getProductCode());
                        pss.setQuantity(pc.getQty());
                        pss.setCreateUser(this.getLoginUserCode());
                        pss.setCreateTime(e);
                        pss.setUpdateTime(e);
                        lst.add(pss);
                    }
                }

                this.pssUseOrderDao.addUseOrder(pssUseOrder, lst, this.pssProductStockDao);
                this.addUserLog("领用", "领用单号:" + pssUseOrder.getUseId());
                return this.getResult(true, "保存完成");
            } catch (Exception var11) {
                this.log.error("add pssUseOrder", var11);
                return this.getResult(false, "领用发生错误");
            }
        }
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "lingyongDateFrom",required = false) String lingyongDateFrom, @RequestParam(value = "lingyongDateTo",required = false) String lingyongDateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, 20000);
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            if(!this.chkNull(shopCode)) {
                cd.eq("UseShop", shopCode);
            }

            if(!this.chkNull(lingyongDateFrom)) {
                cd.ge("UseDate", this.parseDate(lingyongDateFrom, "yyyy-MM-dd"));
            }

            if(!this.chkNull(lingyongDateTo)) {
                cd.le("UseDate", this.parseDate(lingyongDateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }

            this.pssUseOrderDao.getLstUseOrder(e, cd);
            ExportXls xls = new ExportXls(response, "商品领用汇总", "商品领用汇总");
            xls.addHead(new String[]{"领用部门编号", "领用部门名称", "商品编码", "商品名称", "单位", "领用类型", "数量"});
            xls.setWidth(new int[]{10, 25, 10, 25, 10, 10, 10});
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                PssUseOrderTotal puot = (PssUseOrderTotal)i$.next();
                xls.addRow(new Object[]{puot.getUseShop(), this.cache.getValue("shopName", puot.getUseShop()), puot.getProductCode(), this.cache.getValue("ProductcodeList", puot.getProductCode()), this.cache.getValue("productUnit", puot.getUnit()), this.cache.getValue("useType", puot.getUseType()), puot.getQty()});
            }

            xls.close();
            this.addUserLog("导出商品领用汇总", e.getResultList().size() + "条记录");
        } catch (Exception var10) {
            this.log.error("import", var10);
        }

        return null;
    }
}
